package br.com.edumobi.fragments;

import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ProgressBar;
import android.widget.TextView;

import br.com.edumobi.R;

public class LauncherQuestoesFragment extends GenericoFragment {

    private TextView textViewNomeTelaLauncherQuestoesFragment;
    private TextView textViewOrientacaoLauncherQuestoesFragment;
    private TextView textViewUsarAssistenteVirtualLauncherQuestoesFragment;
    private CheckBox checkBoxUsarAssistenteVirtualFragment;
    private Button buttonEstudoLauncherQuestoesFragment;
    private Button buttonSimuladoLauncherQuestoesFragment;
    private ProgressBar progressBarLauncherQuestoesFragment;

    private AcoesLauncherQuestoesFragment activity;
    private LauncherQuestoesTask task;

    private View.OnClickListener clickCheckBoxUsarAssistenteVirtualFragment = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (checkBoxUsarAssistenteVirtualFragment.isChecked()) {
                activity.exibirTexto(R.string.orientacao_usu_assistente_virtual);
            }
        }
    };

    private View.OnClickListener clickButtonEstudoLauncherQuestoesFragment = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            activity.salvarPreferencesUsuAssistenteVirtual(checkBoxUsarAssistenteVirtualFragment.isChecked());
            activity.abrirModoEstudo(checkBoxUsarAssistenteVirtualFragment.isChecked());
        }
    };

    private View.OnClickListener clickButtonSimuladoLauncherQuestoesFragment = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (activity.conectado()) {
                if (task == null || task.getStatus().equals(AsyncTask.Status.FINISHED)) {
                    task = new LauncherQuestoesTask();
                    task.execute();
                }
            } else if (activity.obterQuantidadeDeSimuladosDaClassificacao() > 0) {
                activity.abrirModoSimulado();
            } else {
                activity.falarEExibirTexto(R.string.excecao_sem_conexao_internet);
            }
        }
    };

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        activity = (AcoesLauncherQuestoesFragment) getActivity();
        checkBoxUsarAssistenteVirtualFragment.setChecked(activity.obterPreferencesUsuAssistenteVirtual());
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_launcher_questoes, container, false);

        textViewNomeTelaLauncherQuestoesFragment = (TextView) view.findViewById(R.id.textViewNomeTelaLauncherQuestoesFragment);
        textViewOrientacaoLauncherQuestoesFragment = (TextView) view.findViewById(R.id.textViewOrientacaoLauncherQuestoesFragment);
        textViewUsarAssistenteVirtualLauncherQuestoesFragment = (TextView) view.findViewById(R.id.textViewUsarAssistenteVirtualLauncherQuestoesFragment);
        checkBoxUsarAssistenteVirtualFragment = (CheckBox) view.findViewById(R.id.checkBoxUsarAssistenteVirtualFragment);
        buttonEstudoLauncherQuestoesFragment = (Button) view.findViewById(R.id.buttonEstudoLauncherQuestoesFragment);
        buttonSimuladoLauncherQuestoesFragment = (Button) view.findViewById(R.id.buttonSimuladoLauncherQuestoesFragment);
        progressBarLauncherQuestoesFragment = (ProgressBar) view.findViewById(R.id.progressBarLauncherQuestoesFragment);

        checkBoxUsarAssistenteVirtualFragment.setOnClickListener(clickCheckBoxUsarAssistenteVirtualFragment);
        buttonEstudoLauncherQuestoesFragment.setOnClickListener(clickButtonEstudoLauncherQuestoesFragment);
        buttonSimuladoLauncherQuestoesFragment.setOnClickListener(clickButtonSimuladoLauncherQuestoesFragment);

        return view;
    }

    @Override
    public void exibirComponentes() {
        textViewNomeTelaLauncherQuestoesFragment.setVisibility(View.VISIBLE);
        textViewOrientacaoLauncherQuestoesFragment.setVisibility(View.VISIBLE);
        textViewUsarAssistenteVirtualLauncherQuestoesFragment.setVisibility(View.VISIBLE);
        checkBoxUsarAssistenteVirtualFragment.setVisibility(View.VISIBLE);
        buttonEstudoLauncherQuestoesFragment.setVisibility(View.VISIBLE);
        buttonSimuladoLauncherQuestoesFragment.setVisibility(View.VISIBLE);
        progressBarLauncherQuestoesFragment.setVisibility(View.VISIBLE);
    }

    @Override
    public void esconderComponentes() {
        textViewNomeTelaLauncherQuestoesFragment.setVisibility(View.GONE);
        textViewOrientacaoLauncherQuestoesFragment.setVisibility(View.GONE);
        textViewUsarAssistenteVirtualLauncherQuestoesFragment.setVisibility(View.GONE);
        checkBoxUsarAssistenteVirtualFragment.setVisibility(View.GONE);
        buttonEstudoLauncherQuestoesFragment.setVisibility(View.GONE);
        buttonSimuladoLauncherQuestoesFragment.setVisibility(View.GONE);
        progressBarLauncherQuestoesFragment.setVisibility(View.GONE);
    }

    private class LauncherQuestoesTask extends AsyncTask<Void, Void, Boolean> {

        @Override
        protected void onPreExecute() {
            esconderComponentes();
            progressBarLauncherQuestoesFragment.setVisibility(View.VISIBLE);
            activity.falarTexto(R.string.aguarde);
        }

        @Override
        protected Boolean doInBackground(Void... params) {
            return activity.atualizarSimuladosDaClassificacao();
        }

        @Override
        protected void onPostExecute(Boolean aBoolean) {
            exibirComponentes();
            progressBarLauncherQuestoesFragment.setVisibility(View.GONE);

            if (aBoolean) {
                activity.abrirModoSimulado();
            } else {
                activity.falarEExibirTexto(R.string.excecao_consulta_simulados_malsucedida);
            }
        }
    }

    public interface AcoesLauncherQuestoesFragment extends AcoesGenericoFragment {
        void abrirModoEstudo(boolean usarAssitente);
        void abrirModoSimulado();
        int obterQuantidadeDeSimuladosDaClassificacao();
        boolean atualizarSimuladosDaClassificacao();
    }
}